import { HttpClient, WsClient } from "tsrpc";
import { serverBase } from "../model/server/serverBase";
import { ServiceType, serviceProto } from "../shared/master/serviceProto";
import logic from "./logic";
import word, { wordCfg } from "../model/word/word";
import manage_word from "./model/manage/manage_word";
import { battleDemo } from "../model/fight/demo";
import { loadSkills_auto } from "../config/cfg_sk_auto";
import { loadSkills_active } from "../config/cfg_sk_active";
import app from "../app";
import { bag } from "../model/bag/bag";
import { prop_base } from "../model/bag/prop_base";
import { equip } from "../model/fight/equip/equip";
import { player } from "../model/fight/body/player";
import { prop_min_type } from "../shared/interface";
import god from "../god";
import manage_user from "./model/manage/manage_user";
import manage_team from "./model/manage/manage_team";

class index extends serverBase {
    c_http!: HttpClient<ServiceType>;
    c_wss!: WsClient<ServiceType>;
    constructor() {
        super();
    }
    private startBf() {
        manage_user.init();
        manage_team.init();
        this.loadSkill();
        this.registerDefaultWord();
    }
    async startServer(): Promise<void> {
        this.startBf();
        let cfg = {
            "ip": "127.0.0.1",
            "logger": true,
            "json": true
        }
        const port = 8848;
        const ip = cfg.ip;
        // await this.init();
        // this.c_http = new HttpClient(serviceProto, { server: `http://${ip}:${port}` });
        await this._startServer_wss(serviceProto, { port: port });
        logic.pushFlow();
        // this.c_wss = await serverBase.create_wssClient(serviceProto, {
        //     server: `ws://${ip}:${port}`
        // }) as unknown as WsClient<ServiceType>;
        // this.test()
    }
    private loadSkill() {
        loadSkills_auto();
        loadSkills_active();
    }
    private registerDefaultWord() {
        let cfg = app.getWordCfg('1') as wordCfg;
        manage_word.register(cfg);
    }
    async test() {
        // let data = await db.get(dbKey.player,'a175aa11-8280-4924-826e-765b513cab48')
        // let test = new player()
        // test.reload(data)
        // debugger;


        return;
        // 怪物掉落用例
        let test_equip = new equip();
        test_equip.create_cfg('equip-2', 100,'主神空间');
        // let word = manage_word.getWord('1')
        // let cell = word.getSalfCell();
        // let en = cell.createEnemy();
        // en?.RandomFallDown()

        // new battleDemo().battleStart()
        // return;
        let testPlayer = new player();
        let testBag = testPlayer.getBag();

        // let reslf = app.createCfgItem('reslife-1', 0)
        // testBag.add(reslf);
        // testPlayer.check_wallet(0, 0, [{ id: 'reslife', v: 1 }], true)
        // testBag.test();
        // let expBook = new prop_base({
        //     type:prop_type
        // });

        let equip_item = app.createItem({
            min_type: prop_min_type.装备,
            from:'1',
            num: 1,
            score:100,
            data: test_equip
        })
        testBag.add(equip_item);
        equip_item.use(testPlayer, 0);
        testPlayer.remove_equip(0);

        let ne = testBag.get(0, 1) as prop_base;
        ne.use(testPlayer, 0);
        testPlayer.save()
        // let obj = JSON.parse(JSON.stringify(app.sanitizeObject(test_equip)));
        // let test_equip2 = new equip();
        // test_equip2.reload(obj)
        // debugger;
    }
}
export default new index()._init(__dirname);